import { defineStore } from "pinia";
import storage from "@/utils/storage";
import "@/utils/date.js";
let chatTimeout = null;
function setChatStorage(chatList) {
  clearTimeout(chatTimeout);
  uni.pageScrollTo({
    scrollTop: 999999,
    duration: 0,
  });
  // chatTimeout = setTimeout(() => {
  //   setChatStorage(chatList);
  // }, 1000);
  storage.set("chatList", chatList);
}
export const useChatStore = defineStore("chat", {
  state: () => {
    return {
      chatList: [],
      activeChat: {},
      activeChatIndex: 0,
      isLoading: false,
      id: "", // 本次会话的上下文id，没有这个ai不会连续对话
      conversationId: "",
    };
  },
  actions: {
    setConversation({ id, conversationId }) {
      this.id = id;
      this.conversationId = conversationId;
    },
    resetConversation() {
      this.id = "";
      this.conversationId = "";
    },
    stopAnswer() {
      this.activeChat.isLoading = false;
      this.activeChat.abort();

      this.changeLoading(false);
    },
    setChatStorage(chatList) {
      if (!chatList) {
        chatList = this.chatList;
      }
      clearTimeout(chatTimeout);
      uni.pageScrollTo({
        scrollTop: 999999,
        duration: 0,
      });
      storage.set("chatList", chatList);
    },
    completeAnswer() {
      console.log("ai完成回答");
      // this.activeChat.isLoading = false;
      this.changeLoading(false);

      this.chatList[this.activeChatIndex].isLoading = false;
    },

    changeActiveChat(obj) {
      let { chat, index } = obj;
      this.activeChat = chat;
      if (index != this.activeChatIndex) {
        this.activeChatIndex = index;
      }
    },
    setChatListByStorage(message) {
      let chatList = storage.get("chatList");
      if (chatList) {
        chatList = chatList.map((item) => {
          item.isStorage = true;
          return item;
        });
        this.chatList = chatList;
        setTimeout(() => {
          uni.pageScrollTo({
            scrollTop: 999999,
            duration: 0,
          });
        }, 100);
        // uni.pageScrollTo({
        //   scrollTop: 999999,
        //   duration: 0,
        // });
        if (chatList.length > 1) {
          const lastChatItem = chatList[chatList.length - 1];
          if (lastChatItem.apiType == "stream") {
            this.setConversation(lastChatItem);
          }
        }
      }
    },
    /**
     * @description 添加会话
     * @param {*} message
     * @param {String} message.content
     */
    addChat(message) {
      this.isLoading = true;
      message.time = new Date().Format("yyyy-MM-dd hh:mm:ss");
      this.activeChatIndex = this.chatList.length;
      // message.index
      this.chatList.push(message);

      // setChatStorage(this.chatList);
    },
    refreshChat(obj) {
      this.changeLoading(true);
    },
    changeLoading(val) {
      this.isLoading = val;
      // if (val) {
      //   this.chatList[this.activeChatIndex].content = "正在回答中..";
      // }
    },
    removeChat(message) {
      let { index } = message;
      this.chatList.splice(index, 1);
      this.activeChatIndex = this.chatList.length - 1;
    },
    updateChat(message) {
      debugger;
      let { index, childIndex, thinkking, content } = message;
      let currentIndex =
        index || this.activeChatIndex || this.chatList.length - 1;
      // console.log("chatmodules-currentIndex", index);
      // this.activeChatIndex = currentIndex;
      let chatItem = this.chatList[currentIndex];
      if (childIndex > -1) {
        let key = !content ? "thinkking" : "content";
        chatItem[key][childIndex] = message;
      } else {
        chatItem = Object.assign(chatItem, message);
      }

      this.chatList[currentIndex] = chatItem;
      // }
      this.activeChat = chatItem;
      uni.pageScrollTo({
        scrollTop: 999999,
        duration: 0,
      });
      // 要加点延迟, 不然有可能不生效
      // setTimeout(() => {

      // }, 50);
    },
    clearChat() {
      this.chatList = [];
      this.resetConversation();
      storage.remove("chatList");
      // setChatStorage(this.chatList);
    },
  },
});
